﻿// Downplay.Gravity
// Draggable UI for content building



Downplay.Gravity = {
    DataPrefix: "djax",
    DataDrivers: new Array()
};

(function ($) {

    function onDropped(zone, dropper) {
        var zoneId = zone.attr("data-drop-zone-id");
        var dropId = dropper.attr("data-drop-id");
        // Convert to Ajax request
        var url = window.location;
        $.get(url, {
            djaxOn: true,
            djaxDataDrop: true,
            djaxDataZoneId: zoneId,
            djaxDataDropId: dropId,
            djaxDataBeforeId: "",
            djaxDataAfterId: ""
        }, function (data) {
            alert(data);
        }
        );
    }

    $(".drop-zone[data-drop-zone=true]").droppable({
        drop: function (event, ui) {
            onDropped($(this), $(ui.draggable));
        },
        over: function (event, ui) {
            $(this).addClass("drop-over");
        },
        out: function (event, ui) {
            $(this).removeClass("drop-over");
        },
        activate: function (event, ui) {
            $(this).addClass("drop-accept");
        },
        deactivate: function (event, ui) {
            $(this).removeClass("drop-accept");
            $(this).removeClass("drop-over");
        }
    });

    $(".drop-zone[data-drop-drag-handle=true]").each(function (e) {
        var handle = $(this);
        var dragId = handle.attr("data-drop-drag-id");
        var drag = handle.parents(".drop-zone[data-drop-id=" + dragId + "]").slice(0);
        drag = handle;
        drag.draggable({
            handle: handle,
            revert: true,
            opacity: 0.8,
            helper: 'clone',
            zIndex: 10,
            start: function (event, ui) {
                $("body").attr("data-dragging", "true");
            },
            stop: function (event, ui) {
                $("body").attr("data-dragging", "false");
            }
        });

    });

} (jQuery));